<template>
  <!-- <van-list v-model="loading" :finished="finished" @load="onLoad" class="goods-list">
    <div class="goods-list-box">
      <div v-for="item in goods" :key="item.id" @click="handleClick(item.id)" class="good-box">
        <img :src="item.index_pic" class="good-pic">
        <p class="good-info good-name">{{ item.product_name }}</p>
        <p class="good-info good-desc">{{ item.description }}</p>
        <p class="good-info good-price">¥ {{ item.price }}</p>
      </div>
    </div>
  </van-list> -->
  <!-- <van-tabs class="shop-tabs" @click="changeType">
    <van-tab
      v-for="(item, index) in typeList"
      :title="item.dictLabel"
      :key="index"
    >
      <van-list
        v-model="loading"
        @load="onLoad"
        :finished="finished"
        class="goods-list"
      >
        <div class="goods-list-box">
          <div
            v-for="item in goods"
            :key="item.id"
            @click="handleClick(item.id)"
            class="good-box"
          >
            <img :src="item.src" class="good-pic" />
            <p class="good-info good-name">{{ item.fullName }}</p>
            <p class="good-info good-desc">{{ item.description }}</p>
            <p class="good-info good-price">¥ {{ item.price }}</p>
          </div>
        </div>
      </van-list>
    </van-tab>
  </van-tabs> -->
  <div class="shopContainer">
    <div class="shopleft">
      <nav>
        <ul>
          <li
            @click.stop="changeType(index)"
            v-for="(item, index) in typeList"
            :key="index"
            :class="{ active: activeIndex === index }"
          >
            {{ item.dictLabel }}
          </li>
        </ul>
      </nav>
    </div>
    <div class="shopright">
      <!-- <div class="shoprightsearch">
        <van-cell-group>
          <van-field
            v-model="sms"
            center
            clearable
            left-icon="search"
            label=""
            placeholder="请输入要搜索的药品名称"
          >
            <van-button slot="button" size="small" type="danger" @click="searchbykeyword">搜索</van-button>
          </van-field>
        </van-cell-group>
      </div> -->
      <!-- <div class="shoprightlist"> -->
      <van-list
        v-show="showGoodsList"
        v-model="loading"
        @load="onLoad"
        :finished="finished"
        class="goods-list"
      >
        <div class="goods-list-box">
          <div
            v-for="item in goods"
            :key="item.id"
            @click="handleClick(item.id)"
            class="good-box"
          >
            <img :src="item.src" class="good-pic" />
            <p class="good-info good-name">{{ item.fullName }}</p>
            <p class="good-info good-desc">{{ item.description }}</p>
            <p class="good-info good-price">¥ {{ item.price }}</p>
          </div>
        </div>
      </van-list>
      <div v-show="!showGoodsList" class="noGoodsTips">暂无商品~</div>
      <!-- </div> -->
    </div>
  </div>
</template>

<script>
import { CellGroup, List, Tab, Tabs, Field, Button } from 'vant';
import api from '@/api';
const { product } = api;

export default {
  data() {
    return {
      goods: [],
      loading: false,
      finished: false,
      currentPage: 0,
      pageCount: 0,
      typeList: [],
      showList: false,
      activeIndex: 0,
      sms: '',
      showGoodsList: true,
    };
  },
  components: {
    [CellGroup.name]: CellGroup,
    [List.name]: List,
    [Tab.name]: Tab,
    [Tabs.name]: Tabs,
    [Field.name]: Field,
    [Button.name]: Button,
  },
  created() {
    console.log(this.$route.query);
    this.getType();
  },

  methods: {
    searchbykeyword() {
      console.log(111, this.sms);
    },
    getqueryIndex() {
      if (this.$route.query && this.$route.query.currentindex) {
        this.activeIndex = Number(this.$route.query.currentindex);
      }
    },
    changeType(index) {
      this.loading = true;
      this.showGoodsList = true;
      this.activeIndex = index;
      this.getMedicineList(this.typeList[index].dictValue);
    },
    getType() {
      product.getTypeList().then((res) => {
        if (res && res.code === 200) {
          console.log('typeList', res);
          this.typeList = res.data;
          this.getqueryIndex();
          // console.log(this.activeIndex);
          this.getMedicineList(this.typeList[this.activeIndex].dictValue);
        }
      });
    },
    getMedicineList(type) {
      product
        .getMedList({
          page: 1,
          pageSize: 1000000,
          drugType: type,
        })
        .then((res) => {
          // console.log(res);
          if (res.code !== 200) return;
          this.pageCount = res.total;
          this.goods = res.rows.map((item) => {
            item.src = 'http://www.dzsyiyao.com' + item.pimage;
            return item;
          });

          console.log(this.goods);
          this.loading = false;
          this.showGoodsList =
            this.goods && this.goods.length > 0 ? true : false;
        });
    },

    onLoad() {
      this.loading = true;
      if ((this.goods && this.goods.length) || this.goods.length === 0) {
        this.loading = false;
        this.finished = true;
      }
    },

    handleClick(id) {
      // this.$router.push(`/good-buy/${id}`);
      this.$router.push({
        path: `/good-buy/${id}`,
        query: { activeindex: this.activeIndex },
      });
    },
  },
};
</script>

<style scoped>
.shopContainer {
  display: flex;
  height: calc(100vh - 50px);
  width: 100%;
  overflow: hidden;
}
.shopleft,
.shopright {
  height: 100%;
}
.shopleft {
  flex: 1;
  overflow-y: auto;
}

.shopright {
  flex: 3;
}
.shopleft ul li {
  box-sizing: border-box;
  position: relative;
  padding: 20px 12px;
  overflow: hidden;
  color: #323233;
  font-size: 14px;
  line-height: 20px;
  background-color: #f7f8fa;
  cursor: pointer;
  -webkit-user-select: none;
  user-select: none;
  white-space: normal;
  word-break: keep-all;
  word-wrap: break-word;
}
.shopleft ul li:before {
  position: absolute;
  top: 50%;
  left: 0;
  width: 4px;
  height: 16px;
  background-color: transparent;
  -webkit-transform: translateY(-50%);
  transform: translateY(-50%);
  content: '';
}
.shopright .shoprightsearch {
  position: fixed;
  top: 0;
  right: 0;
  background-color: #f7f8fa;
  height: 55px;
  width: 41vh;
}
.shopright .shoprightlist {
  padding: 10px 10px 0;
  box-sizing: border-box;
  overflow-y: auto;
  height: calc(100% - 50px);
  margin-top: 50px;
}
.shopleft ul li.active:before {
  background-color: #ee0a24;
}
.active {
  background-color: #fff !important;
}
.active .goods-list {
  margin-bottom: 50px;
  padding: 10px;
}
.goods-list-box {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}
.good-box {
  width: 40%;
  margin-bottom: 10px;
  padding: 10px 10px;
  background: #fff;
  box-shadow: 3px 3px 3px #ccc;
  font-size: 16px;
}
.good-pic {
  display: block;
  width: 100%;
  height: 2rem;
}
.good-info {
  margin: 5px 0 0 0;
}
.good-name {
  font-weight: bold;
}
.good-desc {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  font-size: 14px;
}
.noGoodsTips {
  font-size: 16px;
  color: #686868;
  line-height: 20px;
  margin-top: 2vh;
  text-align: center;
}
</style>
